package com.junmo.fanbizhidemo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.junmo.fanbizhidemo.R;
import com.junmo.fanbizhidemo.demoview.CalendarActivity;
import com.junmo.fanbizhidemo.demoview.DateActivity;
import com.junmo.fanbizhidemo.demoview.HengXiangActivity;
import com.junmo.fanbizhidemo.demoview.JhActivity;
import com.junmo.fanbizhidemo.demoview.VideoActivity;
import com.junmo.fanbizhidemo.view.CountdownTextView;

import java.util.Formatter;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by huruihua on 2017/8/25.
 */
public class SettingFragment extends Fragment {
    @BindView(R.id.demo_jh_button)
    Button demoJhButton;
    @BindView(R.id.demo_sp_button)
    Button demoQtButton;
    Unbinder unbinder;
    @BindView(R.id.daojishi)
    CountdownTextView daojishi;

    private long TIME = 60000 * 5;

    //将长度转换为时间
    private StringBuilder mFormatBuilder;
    private Formatter mFormatter;

    private CountDownTimer downTimer = new CountDownTimer(TIME, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            daojishi.setText(stringForTime(Integer.parseInt(String.valueOf(millisUntilFinished))));
        }

        @Override
        public void onFinish() {
//            daojishi.setText("完成");
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_set_layout, container, false);
        unbinder = ButterKnife.bind(this, view);
        mFormatBuilder = new StringBuilder();
        mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
        downTimer.start();

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.demo_jh_button, R.id.demo_sp_button, R.id.demo_date_button, R.id.demo_date_rl_button, R.id.demo_date_hx_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.demo_jh_button:
                startActivity(new Intent(getActivity(), JhActivity.class));
                break;
            case R.id.demo_sp_button:
                startActivity(new Intent(getActivity(), VideoActivity.class));
                break;
            case R.id.demo_date_button:
                startActivity(new Intent(getActivity(), DateActivity.class));
                break;
            case R.id.demo_date_rl_button:
                startActivity(new Intent(getActivity(), CalendarActivity.class));
                break;
            case R.id.demo_date_hx_button:
                startActivity(new Intent(getActivity(), HengXiangActivity.class));
                break;

        }
    }

    /**
     * 将播放长度转化为时间
     *
     * @param timeMs
     * @return
     */
    private String stringForTime(int timeMs) {

        int totalSeconds = timeMs / 1000;

        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        mFormatBuilder.setLength(0);
        if (hours > 0) {
            return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
        } else {
            return mFormatter.format("%02d:%02d", minutes, seconds).toString();
        }
    }

}
